﻿#ifndef __SDL_CANVAS_H__
#define __SDL_CANVAS_H__

#include "SDL_Include.h"
#include "SDL_Platform.h"

/// SDL图像引擎画布类
class SDL_CCanvas : public GAF_CCanvas
{
public:
	SDL_CCanvas();
	~SDL_CCanvas();

protected:
	bool			OnInit(GAF_DWindowHandle hWindow);
	bool			OnRender();

public:
	void			SetGraphicsTree(GAF_CGraphics* pRoot);
	void			ClearGarphics();

private:
	bool			RenderGraphics(GAF_CGraphics* pGraphics);

private:
	struct SGraphicsInfo
	{
		int			nPosX;			// 绘制坐标X
		int			nPosY;			// 绘制坐标Y
		GAF_DAnchor		aAnchor;		// 锚点
		GAF_SPoint		atAnchorOffset;		// 锚点偏移
		BYTE			bAlpha;			// 透明度
		double			dAngle;			// 旋转
		double			dZoomX;			// X轴缩放
		double			dZoomY;			// Y轴缩放
		GAF_EThroughType	through;		// 穿屏模式
		GAF_SRect		rCanvasArea;		// 目标绘制区域
		GAF_CBundle*		pbdlInformation;	// 其他信息
	};

	struct SImageInfo : public SGraphicsInfo
	{
		GAF_DImageHandle	hImage;			// 图像句柄
		SDL_DPixelBit		pbPixelBit;		// 图像像素位数
		GAF_SRect		rClip;			// 图像剪裁区
	};

	struct STextInfo : public SGraphicsInfo
	{
		STD_STRING		strText;		// 文字
		STD_STRING		strFontFile;		// 字体文件
		UINT			nFontSize;		// 字体尺寸
		GAF_DFontStyle		fsFontStyle;		// 字体样式
		int			nSpace;			// 文字间距
		GAF_SColour		cFront;			// 前景颜色
		GAF_SColour		cBack;			// 背景颜色
		GAF_DImageHandle	hTexture;		// 纹理
		UINT			nStrokeSize;		// 描边尺寸
		GAF_SColour		cStroke;		// 描边颜色
	};

	void			SetRenderInfo(GAF_CGraphics* pGraphics, SGraphicsInfo* pGraphicsInfo);

	bool			RenderImage(SImageInfo& iiRender);
	bool			RenderText(STextInfo& tiRender);

	// 绘制图像功能成员
private:
	typedef std::map<STD_STRING, SDL_Surface*>
		MapSurfaceBuffer;
	typedef std::list<STD_STRING>
		LstBufferAdjust;

	MapSurfaceBuffer	m_mapTransformBuffer;	// 图像转换缓存
	LstBufferAdjust		m_lstTransformAdjust;	// 图像转换调节（保留常用，剔除不常用）

	// 渲染
	bool			RenderSurface(SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rSrcClip, SDL_DPixelBit pb, BYTE bAlpha);
	// 以绘制方式
	bool			DrawSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip);
	// 以透明方式
	bool			AlphaSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip, BYTE bAlpha);
	// 以混合方式
	bool			BlendSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip, BYTE bAlpha);

	// 绘制文字功能成员
private:
	std::map<STD_STRING, TTF_Font*>
				m_mapFonts;

	int			ConvertText(const char* pszScrText, char* pszConvText = NULL);
	TTF_Font*		GetFont(STD_STRING strFile, int nSize);
	SDL_Surface*		CreateTextSurface(TTF_Font* pTTF, char* pszText, GAF_SColour cFront, GAF_SColour cBack);

	// 其他功能成员
private:
	SDL_Surface*		AngleZoom(SDL_Surface* pSrcSurface, double dAngle, double dZoomX, double dZoomY);
	SDL_Surface*		MarkSurface(SDL_Surface* pSrcSurface, SDL_Surface* pMarkSurface);
	void			FullFilling(SDL_Surface* pSurface, Uint32 nColour);

	Uint32			AlphaPixel(Uint32 nDisnColour, Uint32 nSrcnColour, SDL_DPixelBit pbSrc, BYTE bAlpha);
	inline Uint32		GetPixel(SDL_Surface* pDisSurface, int nX, int nY);
	inline void		SetPixel(SDL_Surface* pDisSurface, int nX, int nY, Uint32 nColour);

	inline bool		LockSurface(SDL_Surface* pSurface);
	inline void		UnlockSurface(SDL_Surface* pSurface);

private:
	GAF_SRect		m_rMainArea;
	SDL_Surface*		m_pMainSurface;
	GAF_CGraphics*		m_pRoot;

};

#endif